﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class ICACDeptDoc
    {

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_ICACDeptDoc(");
            strSql.Append("FEntityNo,FDeptNo,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FInsDate)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FDeptNo,@FAttachSvrFileName,@FAttachOldFileName,@FAttachDesc,@FAttachFileExt,@FInsDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FDeptNo;
            parameters[2].Value = model.FAttachSvrFileName;
            parameters[3].Value = model.FAttachOldFileName;
            if (model.FAttachDesc == "")
            {
                parameters[4].Value = DBNull.Value;
            }
            else
            {
                parameters[4].Value = model.FAttachDesc;
            }
            parameters[5].Value = model.FAttachFileExt;
            parameters[6].Value = model.FInsDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet GetPath()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select *");
            strSql.Append(" from dbo.t_ICACDeptDoc as a inner join t_HRDept as b");
            strSql.Append(" on a.FDeptNo = b.FDeptNo");
            strSql.Append(" order by FSeq asc");

            return DbHelperSQL.Query(strSql.ToString());

        }



        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="fEntityNo"></param>
        /// <param name="fDeptNo"></param>
        /// <returns></returns>
        public bool Exists(string fEntityNo, string fDeptNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_ICACDeptDoc");
            strSql.Append(" where FEntityNo=@FEntityNo and FDeptNo=@FDeptNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fDeptNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ICACDeptDoc set ");
            strSql.Append("FAttachSvrFileName=@FAttachSvrFileName,");
            strSql.Append("FAttachOldFileName=@FAttachOldFileName,");
            strSql.Append("FAttachDesc=@FAttachDesc,");
            strSql.Append("FAttachFileExt=@FAttachFileExt,");
            strSql.Append("FInsDate=@FInsDate");
            strSql.Append(" where FICACDeptDocID=@FICACDeptDocID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FICACDeptDocID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FAttachSvrFileName;
            parameters[1].Value = model.FAttachOldFileName;
            if (model.FAttachDesc == "")
            {
                parameters[2].Value = DBNull.Value;
            }
            else {
                parameters[2].Value = model.FAttachDesc;
            }
            parameters[3].Value = model.FAttachFileExt;
            parameters[4].Value = model.FInsDate;
            parameters[5].Value = model.FICACDeptDocID;
            parameters[6].Value = model.FEntityNo;
            parameters[7].Value = model.FDeptNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc GetModel(string fEntityNo, string fDeptNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FICACDeptDocID,FEntityNo,FDeptNo,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FInsDate from dbo.t_ICACDeptDoc ");
            strSql.Append(" where FEntityNo=@FEntityNo and FDeptNo=@FDeptNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12)	
			};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fDeptNo;

            ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc model = new ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc model = new ElingOA6_Admin_DSJCJ.Model.ICACDeptDoc();
            if (row != null)
            {
                if (row["FICACDeptDocID"] != null && row["FICACDeptDocID"].ToString() != "")
                {
                    model.FICACDeptDocID = decimal.Parse(row["FICACDeptDocID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FAttachSvrFileName"] != null)
                {
                    model.FAttachSvrFileName = row["FAttachSvrFileName"].ToString();
                }
                if (row["FAttachOldFileName"] != null)
                {
                    model.FAttachOldFileName = row["FAttachOldFileName"].ToString();
                }
                if (row["FAttachDesc"] != null)
                {
                    model.FAttachDesc = row["FAttachDesc"].ToString();
                }
                if (row["FAttachFileExt"] != null)
                {
                    model.FAttachFileExt = row["FAttachFileExt"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得多条图片存放路径
        /// </summary>
        /// <param name="strAdCds"></param>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public List<string> GetSrvName(string dept, string entityNo)
        {
            List<string> listStr = new List<string>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAttachSvrFileName");
            strSql.Append(" FROM dbo.t_ICACDeptDoc  where FDeptNo in('");
            strSql.Append(dept);
            strSql.Append("')");
            strSql.Append(" and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.VarChar,50)};
            parameters[0].Value = entityNo;
            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters))
            {
                while (sdr.Read())
                {
                    listStr.Add(sdr[0].ToString());
                }
            }

            return listStr;
        }




    }
}
